﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataWinner.Nba.DataAccess.Sina;
using DataWinner.Entity.Nba_Sina.Models;
using EntityFramework.Extensions;


namespace DataWinner.Business.Manage
{
    public class BsSinaData
    {
        DaTeam daTeam = new DaTeam();        
        DaMatch daMatch = new DaMatch();
        DaTeamStat daTeamStat = new DaTeamStat();
        DaTeamStatDaily daTeamStatDaily = new DaTeamStatDaily();

        public bool DataClear()
        {
            bool result = false;

            try
            {
                daTeamStatDaily.Clear();
                daTeamStat.Clear();
                daMatch.Clear();
                daTeam.Clear();
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            return result;
        }

        public bool WrongDataClear()
        {
            bool result = false;

            Nba_SinaContext context = new Nba_SinaContext();

            //int count = context.TeamStats.Count(s => s.Match.MatchTime < System.DateTime.Now && s.Scoring == 0);
            context.TeamStat_Sina.Where(s => s.Match_Sina.MatchTime < System.DateTime.Now && s.Scoring == 0).Delete();
            context.SaveChanges();

            var matchIdSet = context.TeamStat_Sina.Select(s => s.MatchId);
            context.Match_Sina.Where(m => matchIdSet.Contains(m.MatchId)).Delete();
            context.SaveChanges();
            

            
            return result;
        }

        //public List<TeamStatAvg> CountTeamStatAvg(DateTime? statDate)
        //{
        //    List<TeamStatAvg> result = new List<TeamStatAvg>();

        //    DaTeamStat daTeamStat = new DaTeamStat();

        //    //List<TeamStat> teamStatSet = new List<TeamStat>();
        //    List<Match> matchSet = new List<Match>();
            
        //    if (statDate == null)
        //    {
        //        matchSet = daMatch.Set.Include(m=>m.Team).Include(m=>m.Team1)
        //            .Include(m=>m.Team.TeamStats).Include(m=>m.Team1.TeamStats).ToList();
        //    }
        //    else            
        //    {
        //        matchSet = daMatch.Set.Where(s => s.MatchTime < statDate).Include(m=>m.Team1)
        //            .Include(m=>m.Team.TeamStats).Include(m=>m.Team1.TeamStats).ToList();
        //    }

        //    //key:TeamId-StatType-StatTime
        //    //1:as home team; 2: opposite when as home team; 3: as visiting team; 4: opposite when as visiting team;
        //    Dictionary<string, TeamStatAvg> dicTeamStatAvg = new Dictionary<string, TeamStatAvg>();

        //    foreach (Match match in matchSet)
        //    {

        //    }

        //    return result;
        //}
    }
}
